using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bridge : MonoBehaviour, ItemTriggerReceiver
{
    [Header("基础配置")]
    [Tooltip("桥面预制件")]
    public GameObject bridgeFloorPrefab;
    [Tooltip("桥面数量")]
    public int bridgeFloorCount = 2;
    [Tooltip("桥面数量")]
    public float boomIntervalTime = 60f/60f;


    [Header("调试")]
    public BridgeFloor.EnumState bridgeFloorState = BridgeFloor.EnumState.Complete;

    private bool booming;
    private float startBoomTime;
    private SpriteRenderer sr;
    private List<BridgeFloor> bridgeFloors;
    private void Awake()
    {
        sr=GetComponent<SpriteRenderer>();
        sr.enabled = false;
        ReBuildBridge();
    }

    void Start()
    {

    }

    void Update()
    {

    }

    private void FixedUpdate()
    {
        UpdateBoom();
    }

    private void ReBuildBridge()
    {
        int count = transform.childCount;
        bridgeFloors = new List<BridgeFloor>();
        for (int i = count - 1; i >= 0; i--)
        {
            Transform childTransform = transform.GetChild(i);
            if (childTransform.GetComponent<BridgeFloor>() != null)
            {
                //UnityEditor.EditorApplication.delayCall += () =>
                DestroyImmediate(childTransform.gameObject);
            }
        }
        for (int i = 0; i < bridgeFloorCount; i++)
        {
            GameObject bridgeFloorObj = Instantiate(bridgeFloorPrefab, transform);
            BridgeFloor bridgeFloor = bridgeFloorObj.GetComponent<BridgeFloor>();
            bridgeFloors.Add(bridgeFloor); 
            float x = -bridgeFloorCount + (i * 2) + 1;
            bridgeFloorObj.transform.localPosition = new Vector2(x, 0);

            BridgeFloor.EnumBridgeFloorType bridgeFloorType = BridgeFloor.EnumBridgeFloorType.Middle;
            if (i == 0 && i != bridgeFloorCount - 1)
            {
                bridgeFloorType = BridgeFloor.EnumBridgeFloorType.Left;
            }
            else if (i != 0 && i == bridgeFloorCount - 1)
            {
                bridgeFloorType = BridgeFloor.EnumBridgeFloorType.Right;
            }
            bridgeFloor.bridgeFloorType = bridgeFloorType;
            bridgeFloor.bridgeFloorState = bridgeFloorState;
            string typeMame = bridgeFloorType == BridgeFloor.EnumBridgeFloorType.Left ? "左" : (bridgeFloorType == BridgeFloor.EnumBridgeFloorType.Middle ? "中" : "右");
            bridgeFloorObj.name = string.Format("桥面{0}-{1}", i, typeMame);
            bridgeFloor.OnValidateByEditorCall();

        }
    }

    private void UpdateBoom()
    {
        if (booming) {
            float dt = Time.time - startBoomTime;
            int index=Mathf.FloorToInt(dt / boomIntervalTime);

            for(int i=0;i<bridgeFloorCount && i <= index; i++)
            {
                BridgeFloor bridgeFloor = bridgeFloors[i];
                bridgeFloor.StartBoom();
            }
        }
    }

    public void StartBoom()
    {
        if (!booming)
        {
            booming = true;
            startBoomTime = Time.time;
        }
    }
    public virtual void OnTriggerEnter2D(Collider2D collision)
    {
        if (Utility.IsLayer(collision.gameObject, "Player"))
        {
            StartBoom();
        }
    }

    public virtual void OnTriggerExit2D(Collider2D collision)
    {
    }

    public virtual void OnTriggerStay2D(Collider2D collision)
    {
    }

    public virtual void OnCollisionEnter2D(Collision2D collision)
    {
    }

    public virtual void OnCollisionExit2D(Collision2D collision)
    {
    }

    public virtual void OnCollisionStay2D(Collision2D collision)
    {
    }
}
